package com.hnyfkj.jyindustry.biz.sysrole.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hnyfkj.jyindustry.biz.sysrole.dto.SysRoleInsertDto;
import com.hnyfkj.jyindustry.biz.sysrole.dto.SysRoleQueryDto;
import com.hnyfkj.jyindustry.biz.sysrole.dto.SysRoleUpdateDto;
import com.hnyfkj.jyindustry.biz.sysrole.entity.SysRoleEntity;
import com.hnyfkj.jyindustry.biz.sysrole.service.SysRoleService;
import com.hnyfkj.jyindustry.common.utils.PageUtils;
import com.hnyfkj.jyindustry.common.utils.Response;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @author zjq
 *
 */
@Controller
@RequestMapping("sysRole")
@Api(tags = {"角色表"})
public class SysRoleController {
    @Autowired
    private SysRoleService sysRoleService;
    
    /**
	 * 添加页面
	 */
	@GetMapping("/add")
	public String addPage() {
		return "sys_role/role-add";
	}

	/**
	 * 编辑页面
	 */
	@GetMapping("/edit")
	public String editPage(@RequestParam("id") Integer id, Model model) {
		SysRoleEntity sysRoleEntity = sysRoleService.getById(id);
		model.addAttribute("sysRoleEntity", sysRoleEntity);
		return "sys_role/role-edit";
	}

	/**
	 * 列表页面
	 */
	@GetMapping("/listPage")
	public String listPage() {
		return "sys_role/role-table";
	}
	
	/**
	 * 查看权限
	 */
	@GetMapping("/menuTree")
	public String menuTree(@RequestParam("id") Integer id, Model model) {
		model.addAttribute("roleId", id+"");
		return "sys_role/role-menuTree";
	}

    /**
     * 列表
     */
    @PostMapping("/list")
    @ResponseBody
    @ApiOperation(value = "列表查看", produces = "application/json")
    public Response<PageUtils<SysRoleEntity>> list(@RequestBody final SysRoleQueryDto queryDto) {
  		PageUtils<SysRoleEntity> ls = sysRoleService.selectListDto(queryDto);
    	return Response.success(ls.getCount(), ls.getList());
    }


    /**
     * 新增
     */
    @PostMapping("/save")
    @ResponseBody
    @ApiOperation(value = "新增", produces = "application/json")
    public Response save(@RequestBody SysRoleInsertDto sysRoleDto) {
        sysRoleService.insert(sysRoleDto);
        return Response.success();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @ResponseBody
    @ApiOperation(value = "修改", produces = "application/json")
    public Response update(@RequestBody final SysRoleUpdateDto sysRole) {
        sysRoleService.update(sysRole);
        return Response.success();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    @ResponseBody
    @ApiOperation(value = "删除")
    public Response delete(@RequestParam("ids[]") final Integer[] roleIds) {
        sysRoleService.delectBatch(roleIds);
        return Response.success();
    }
    
    /**
     * 查询全部角色
     */
    @PostMapping("/roleList")
    @ResponseBody
    @ApiOperation(value = "查询全部角色")
    public List<SysRoleEntity> getRoleList() {
        return sysRoleService.getRoleList();
    }

}
